<?php
/*
 * image.php
 *
 * Copyright (C) 2006 - OS3 srl - http://www.os3.it
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this software; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
require_once ( "upload.php" );

function & image_get_sizes ( $fname )
{
	$ext = upload_extension ( false, $fname );

	switch ( $ext )
	{
		case "jpg":
			$src = @ imagecreatefromjpeg ( $fname );
			break;
		case "png":
			$src = @ imagecreatefrompng ( $fname );
			break;

		case "gif":
			$src = @ imagecreatefromgif ( $fname );
			break;

		default:
			util_display_error ( "Invalid image format. Only JPEG and PNG are supported", $fname );
			return ( false );
	}

	if ( $src == false )
	{
		util_display_error ( "Cannot load image $fname." );
		return ( false );
	}

	$sx = imagesx ( $src );
	$sy = imagesy ( $src );

	$a = array ( $sx, $sy, $src );
	return $a;
}

function image_scale ( $fname, $dest_name, $dx = 100, $dy = 100, $jpeg_quality = 70 )
{
	$arr = & image_get_sizes ( $fname );

	if ( $arr == false ) return ( false );

	$sx = $arr [ 0 ];
	$sy = $arr [ 1 ];
	$src = & $arr [ 2 ];
	
	list ( $img_x, $img_y ) = __calc_size ( $sx, $sy, $dx, $dy );

	$dest = @imagecreatetruecolor ( $img_x, $img_y );

	if ( $dest == false )
	{
		util_display_error ( "Cannot create the destination image for some reason" );
		imagedestroy ( $src );
		return ( false );
	}

	if ( imagecopyresampled ( $dest, $src, 0, 0, 0, 0, $img_x, $img_y, $sx, $sy ) == false )
	{
		util_display_error ( "Cannot resize the image" );
		imagedestroy ( $src );
		imagedestroy ( $dest );
		return ( false );
	}

	$ext = upload_extension ( false, $dest_name );

	switch ( $ext )
	{
		case "jpg":
			$res = imagejpeg ( $dest, $dest_name, $jpeg_quality );
			break;
	
		case "png":
			$res = imagepng ( $dest, $dest_name );
			break;

		case "gif":
			$res = imagegif ( $dest, $dest_name );
			break;

		default:
			util_display_error ( "Invalid image format. Only JPEG and PNG are supported", $dest_name );
			return ( false );
	}

	imagedestroy ( $src );
	imagedestroy ( $dest );

	if ( $res == false )
	{
		util_display_error ( "Cannot save the image", $dest );
		return ( false );
	}
	
	chmod ( $dest_name, 0644 );

	return ( true );
}

// INTERNALS
function __calc_size ( $sx, $sy, $dx, $dy )
{
	$img_x = 0;
	$img_y = 0;

	if ( $sx > $sy )
	{
		$img_x = $dx;
		$img_y = round ( ( $dx * $sy ) / $sx );
	} else {
		$img_x = round ( ( $dy * $sx ) / $sy );
		$img_y = $dy;
	}

	return ( array ( $img_x, $img_y ) );
}
?>
